﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Common;
using System.Data;
using System.Data.SqlClient;
using SYH.Config;

namespace SYH.System.DAL
{
    /// <summary>
    /// 数据访问类:SystemAction
    /// </summary>
    public class SystemAction
    {
        public SystemAction()
        { }
        #region  基本方法
        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int Id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from SystemAction");
            strSql.Append(" where Id=@Id");
            SqlParameter[] parameters = {
					new SqlParameter("@Id", SqlDbType.Int,4)
			};
            parameters[0].Value = Id;

            object obj = SqlHelper.ExecuteScalar(WebConfig.SqlConnString, CommandType.Text, strSql.ToString(), parameters);
            return Common.TypeParse.ObjectToInt(obj, 0) > 0;
        }


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(SYH.System.Model.SystemAction model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into SystemAction(");
            strSql.Append("ControllerId,Action,Description)");
            strSql.Append(" values (");
            strSql.Append("@ControllerId,@Action,@Description)");
            strSql.Append(";select @@IDENTITY");
            SqlParameter[] parameters = {
					new SqlParameter("@ControllerId", SqlDbType.Int,4),
					new SqlParameter("@Action", SqlDbType.VarChar,50),
					new SqlParameter("@Description", SqlDbType.NVarChar,50)};
            parameters[0].Value = model.ControllerId;
            parameters[1].Value = model.Action;
            parameters[2].Value = model.Description;

            object obj = SqlHelper.ExecuteScalar(WebConfig.SqlConnString, CommandType.Text, strSql.ToString(), parameters);
            return Common.TypeParse.ObjectToInt(obj, 0);
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(SYH.System.Model.SystemAction model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update SystemAction set ");
            strSql.Append("ControllerId=@ControllerId,");
            strSql.Append("Action=@Action,");
            strSql.Append("Description=@Description");
            strSql.Append(" where Id=@Id");
            SqlParameter[] parameters = {
					new SqlParameter("@ControllerId", SqlDbType.Int,4),
					new SqlParameter("@Action", SqlDbType.VarChar,50),
					new SqlParameter("@Description", SqlDbType.NVarChar,50),
					new SqlParameter("@Id", SqlDbType.Int,4)};
            parameters[0].Value = model.ControllerId;
            parameters[1].Value = model.Action;
            parameters[2].Value = model.Description;
            parameters[3].Value = model.Id;

            int rows = SqlHelper.ExecuteNonQuery(WebConfig.SqlConnString, CommandType.Text, strSql.ToString(), parameters);
            return rows > 0;
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int Id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from SystemAction ");
            strSql.Append(" where Id=@Id");
            SqlParameter[] parameters = {
					new SqlParameter("@Id", SqlDbType.Int,4)
			};
            parameters[0].Value = Id;

            int rows = SqlHelper.ExecuteNonQuery(WebConfig.SqlConnString, CommandType.Text, strSql.ToString(), parameters);
            return rows > 0;
        }
        /// <summary>
        /// 批量删除数据
        /// </summary>
        public bool DeleteList(string Idlist)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from SystemAction ");
            strSql.Append(" where Id in (" + Idlist + ")  ");
            int rows = SqlHelper.ExecuteNonQuery(WebConfig.SqlConnString, CommandType.Text, strSql.ToString());
            return rows > 0;
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public SYH.System.Model.SystemAction GetModel(int Id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 Id,ControllerId,Action,Description from SystemAction ");
            strSql.Append(" where Id=@Id");
            SqlParameter[] parameters = {
					new SqlParameter("@Id", SqlDbType.Int,4)
			};
            parameters[0].Value = Id;

            SYH.System.Model.SystemAction model = new SYH.System.Model.SystemAction();
            DataSet ds = SqlHelper.ExecuteDataset(WebConfig.SqlConnString, CommandType.Text, strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                return DataRowToModel(ds.Tables[0].Rows[0]);
            }
            else
            {
                return null;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public SYH.System.Model.SystemAction DataRowToModel(DataRow row)
        {
            SYH.System.Model.SystemAction model = new SYH.System.Model.SystemAction();
            if (row != null)
            {
                if (row["Id"] != null && row["Id"].ToString() != "")
                {
                    model.Id = int.Parse(row["Id"].ToString());
                }
                if (row["ControllerId"] != null)
                {
                    model.ControllerId = int.Parse(row["ControllerId"].ToString());
                }
                if (row["Action"] != null)
                {
                    model.Action = row["Action"].ToString();
                }
                if (row["Description"] != null)
                {
                    model.Description = row["Description"].ToString();
                }
            }
            return model;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select Id,ControllerId,Action,Description ");
            strSql.Append(" FROM SystemAction ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return SqlHelper.ExecuteDataset(WebConfig.SqlConnString, CommandType.Text, strSql.ToString());
        }

        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ");
            if (Top > 0)
            {
                strSql.Append(" top " + Top.ToString());
            }
            strSql.Append(" Id,ControllerId,Action,Description ");
            strSql.Append(" FROM SystemAction ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by " + filedOrder);
            return SqlHelper.ExecuteDataset(WebConfig.SqlConnString, CommandType.Text, strSql.ToString());
        }

        /// <summary>
        /// 获取记录总数
        /// </summary>
        public int GetRecordCount(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) FROM SystemAction ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            object obj = SqlHelper.ExecuteNonQuery(WebConfig.SqlConnString, CommandType.Text, strSql.ToString());
            return Common.TypeParse.ObjectToInt(obj, 0);
        }

        #endregion  BasicMethod
    }
}
